package conson.tech.manage.common.service;


import conson.tech.manage.common.util.FtpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


/**
 * Description: FTP文件上传服务类
 * Author: HXL
 * Date: 2020/3/27 11:04 上午
 * Version: 1.0
 */
@Slf4j
@Service("fileUploadService")
public class FileUploadService {
    @Value("${ftp.http}")
    private String HTTP_NAME;
    /**
     * ftp服务器ip地址
     */
    @Value("${ftp.serverIP}")
    private String FILE_SERVER_IP;
    /**
     * ftp服务器用户名
     */
    @Value("${ftp.loginName}")
    private String LOGIN_NAME;
    /**
     * ftp服务器密码
     */
    @Value("${ftp.password}")
    private String LOGIN_PASSWORD;
    /**
     * Title: 将文件上传到FTP
     * Description:
     * @param file
     * @param path
     * @return: java.lang.String
     * Company: GX
     * Author HXL
     * Date 2020/4/16 3:45 下午
     */
    public String fileUpload(File file, String path) {
        FtpUtils ftpUtils = getFtpUtils("");
        try {
            if (!ftpUtils.isConnected()) {
                ftpUtils.connect();
            }
            return HTTP_NAME+ftpUtils.uploadFile(file.getName(), new FileInputStream(file), path);
        } catch (IOException e) {
            log.error("FileUploadService--> fileUpload fail. params:fileName-->[{}],path-->[{}]",file==null?null:file.getName(),path, e);
        }
        return null;
    }
    public String fileUpload(MultipartFile file, String path) {
        FtpUtils ftpUtils = getFtpUtils("");
        try {
            if (!ftpUtils.isConnected()) {
                ftpUtils.connect();
            }
            return HTTP_NAME+ftpUtils.uploadFile(file.getOriginalFilename(), file.getInputStream(), path);
        } catch (IOException e) {
            log.error("FileUploadService--> fileUpload fail. params:fileName-->[{}],path-->[{}]",file==null?null:file.getOriginalFilename(),path, e);
        }
        return null;
    }
    /**
     * Title: 根据路径和文件名下载文件并经过Base64编码
     * Description:
     * @param path
     * @param fileName
     * @return: java.lang.String
     * Company: GX
     * Author HXL
     * Date 2020/4/16 3:39 下午
     */
    public String download(String path, String fileName){
        FtpUtils ftpUtils = getFtpUtils("");
        try {
            if (!ftpUtils.isConnected()) {
                ftpUtils.connect();
            }
            return ftpUtils.download( path,fileName);
        } catch (IOException e) {
            log.error("FileUploadService--> download fail. params:fileName-->[{}],path-->[{}]",fileName,path, e);
        }
        return null;
    }

    public FtpUtils getFtpUtils(String path) {
        return FtpUtils.createFtpCli(FILE_SERVER_IP, LOGIN_NAME, LOGIN_PASSWORD, path);
    }

    public String getHTTP_NAME() {
        return HTTP_NAME;
    }

    public String getFILE_SERVER_IP() {
        return FILE_SERVER_IP;
    }
}
